package com.sport.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sport.bean.Admin;
import com.sport.bean.PageParam;
import com.sport.bean.PageResult;
import com.sport.bean.Project;
import com.sport.service.IProjectService;
import com.sport.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author wxq
 * @Date 2021/5/8
 **/

@Controller
public class ProjectController {
    @Autowired
    private IProjectService iProjectService;


    @GetMapping(value = "/project/list")
    @ResponseBody
    public PageResult<Project> findAll1(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                        @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                                        HttpServletRequest request) {
        PageHelper.startPage(page, limit);
        PageParam<Project> pageParam = new PageParam<>(request);
        List<Project> projects = iProjectService.listPage(pageParam);
        PageInfo info = new PageInfo(projects);
        return new PageResult<Project>(projects, info.getTotal());
    }

    /*
    删除
    */
    @GetMapping("/project/del")
    @ResponseBody
    public JsonResult delAdmin(Integer id) {
        if (iProjectService.deleteProject(id) > 0) {
            return JsonResult.ok("删除成功");
        } else {
            return JsonResult.error("删除失败");
        }
    }

    /**
     * id查找
     */
    @GetMapping("/project/findId")
    @ResponseBody
    public Project findId(Integer id) {
        System.out.println(iProjectService.findById(id));
        return iProjectService.findById(id);

    }

    /**
     * 修改参赛信息
     */

    @GetMapping("/project/update")
    @ResponseBody
    public JsonResult updatProject(MultipartFile file, Project project) {
        if ( iProjectService.editProject(project) > 0) {
            return JsonResult.ok("修改成功");
        } else {
            return JsonResult.error("修改失败");
        }
}
    /**
    修改参赛状态
     */
        @GetMapping("/project/updatestate")
        @ResponseBody
        public JsonResult updatProjectState(MultipartFile file, Integer id,Integer state) {
            if ( iProjectService.updateState(id,state) > 0) {
                return JsonResult.ok("修改成功");
            } else {
                return JsonResult.error("修改成功");
            }


    }
    /**
     *添加项目
     */
    @GetMapping("/project/add")
    @ResponseBody
    public JsonResult addProject(MultipartFile file, Project project) {


        if ( iProjectService.addProject(project) > 0) {
            return JsonResult.ok("添加成功");
        } else {
            return JsonResult.error("添加失败");
        }

    }

    //修改参赛状态



}